package com.stud.system.controller.workflow;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stud.common.annotation.Log;
import com.stud.common.core.controller.BaseController;
import com.stud.common.core.domain.Result;
import com.stud.common.core.page.TableDataInfo;
import com.stud.common.enums.BusinessType;
import com.stud.common.enums.FileBusinessType;
import com.stud.common.utils.poi.ExcelUtil;
import com.stud.system.domain.WorkflowLog;
import com.stud.system.service.FileLinkService;
import com.stud.system.service.WorkflowLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 流程审批记录Controller
 *
 * @author Stud
 * @date 2023-03-06
 */
@Api(tags = "流程审批记录")
@RestController
@RequestMapping("/system/workflowLog")
@RequiredArgsConstructor
public class WorkflowLogController extends BaseController {

    private final WorkflowLogService workflowLogService;

    private final FileLinkService fileLinkService;

    @ApiOperation("查询流程审批记录列表")
    @GetMapping("/list")
    public TableDataInfo<WorkflowLog> list(@RequestParam Long workflowId) {
        List<WorkflowLog> list = workflowLogService.listByWorkflowId(workflowId);

        List<Long> idList = list.stream().map(WorkflowLog::getId).collect(Collectors.toList());
        Map<Long, List<Long>> fileIdMap = fileLinkService.fileLinkIdMap(FileBusinessType.WORKFLOW_TASK_LOG, idList);
        list.forEach(temp -> {
            if (fileIdMap.containsKey(temp.getId())) {
                temp.setFileLinkIdList(fileIdMap.get(temp.getId()));
            }
        });

        return getDataTable(list);
    }

    @ApiOperation("导出流程审批记录列表")
    @Log(title = "流程审批记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkflowLog workflowLog) {
        Wrapper<WorkflowLog> wrapper = new QueryWrapper(workflowLog);
        List<WorkflowLog> list = workflowLogService.list(wrapper);
        ExcelUtil<WorkflowLog> util = new ExcelUtil<>(WorkflowLog.class);
        util.exportExcel(response, list, "流程审批记录数据");
    }

    @ApiOperation("获取流程审批记录详细信息")
    @GetMapping(value = "/{id}")
    public Result<WorkflowLog> getInfo(@PathVariable("id") Long id) {
        return Result.ok(workflowLogService.getById(id));
    }

    @ApiOperation("删除流程审批记录")
    @Log(title = "流程审批记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result<Boolean> remove(@PathVariable Long[] ids) {
        return Result.ok(workflowLogService.removeByIds(ids));
    }
}
